﻿using System;
using System.Collections.Generic;

namespace DealPoker
{
    /// <summary>
    
    public class PokerManager
    {
        #region 单例类

        private static PokerManager _instance = null;

        private PokerManager()
        {

        }

        public static PokerManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new PokerManager();
                }
                return _instance;
            }
        }

        #endregion

        //存放所有的扑克牌
        public List<Poker> allPoker = new List<Poker>();
        //存放每个人的扑克牌集合
        public List<Poker> player01 = new List<Poker>();
        public List<Poker> player02 = new List<Poker>();
        public List<Poker> player03 = new List<Poker>();
        public List<Poker> player04 = new List<Poker>();

        /// <summary>
        /// 创建扑克
        /// </summary>
        public void CreatePoker()
        {
            //外层循环控制花色，内层循环控制牌面大小
            for (int i = -4; i < 0; i++)
            {
                for (int j = 3; j < 16; j++)
                {
                    Array colorAry = Enum.GetValues(typeof(PokerColor));
                    Array numberAry = Enum.GetValues(typeof(PokerNumber));

                    PokerColor p_color = (PokerColor)colorAry.GetValue(i + 4);
                    PokerNumber p_number = (PokerNumber)numberAry.GetValue(j - 3);

                    //实例化一张扑克牌
                    Poker poker = new Poker(p_color, p_number);
                    allPoker.Add(poker);
                }
            }
        }

        //依次发牌
        public void SendPokerToPlayer()
        {
            //创建随机对象
            Random random = new Random();
            //每个人拿到的牌数
            int count = allPoker.Count / 4;
            //设置发牌
            for (int i = 0; i < count; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    //随机下标
                    int index = random.Next(0, allPoker.Count);
                    //根据下标依次发牌
                    switch (j)
                    {
                        case 0:
                            player01.Add(allPoker[index]);
                            break;
                        case 1:
                            player02.Add(allPoker[index]);
                            break;
                        case 2:
                            player03.Add(allPoker[index]);
                            break;
                        case 3:
                            player04.Add(allPoker[index]);
                            break;
                        default:
                            break;
                    }
                    allPoker.RemoveAt(index);
                }
            }
        }

        //打印所有玩家的扑克
        public void PrintPokerOfAllPlayer()
        {
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("第{0}个人的牌是：", i + 1);
                for (int j = 0; j < player01.Count; j++)
                {
                    switch (i)
                    {
                        case 0:
                            player01[j].PrintPokerInfo();
                            break;
                        case 1:
                            player02[j].PrintPokerInfo();
                            break;
                        case 2:
                            player03[j].PrintPokerInfo();
                            break;
                        case 3:
                            player04[j].PrintPokerInfo();
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        //打印某个人的牌
        public void PrintSomeonePoker(List<Poker> p)
        {
            for (int i = 0; i < p.Count; i++)
            {
                p[i].PrintPokerInfo();
            }

        }


        //按照花色进行排序
        public void OrderPokerByColor(List<Poker> p_List)
        {
            //方法一：
            //            p_List.Sort (SuitCompare);
            //方法二：匿名委托
            //            p_List.Sort (delegate(Poker x, Poker y) {
            //                int a = GetPokerValueByColor (x);
            //                int b = GetPokerValueByColor (y);
            //                return b.CompareTo (a);
            //            });
            //            p_List.Sort (delegate(Poker A, Poker B) {
            //                return -A.p_Number.CompareTo (B.p_Number);
            //            });
            //方法三：Lambda表达式
            p_List.Sort((Poker x, Poker y) => {
                return GetPokerValueByColor(y).CompareTo(GetPokerValueByColor(x));
            });
        }

        //
        int SuitCompare(Poker A, Poker B)
        {
            //获取扑克A和B的新的数值大小
            int a = GetPokerValueByColor(A);
            int b = GetPokerValueByColor(B);
            return b - a;
        }

        //按照花色获取某张扑克代表的数值，
        //比较两个三位数，先比较百位，其次是十位和个位
        //百位代表的是花色，十位和个位表示牌面大小
        int GetPokerValueByColor(Poker p)
        {
            //获取扑克的花色
            PokerColor c = p.p_Color;
            //记录每个花色的代表值
            int value = 0;
            switch (c)
            {
                case PokerColor.黑桃:
                    value = 400;
                    break;
                case PokerColor.红心:
                    value = 300;
                    break;
                case PokerColor.梅花:
                    value = 200;
                    break;
                case PokerColor.方块:
                    value = 100;
                    break;
                default:
                    break;
            }

            //牌的大小
            PokerNumber num = p.p_Number;
            //记录每张牌新的大小（花色值+牌面值）
            return value + (int)num;
        }


        //按照大小进行排序
        public void OrderPokerByNumber(List<Poker> p_List)
        {
            p_List.Sort(delegate (Poker x, Poker y) {
                int a = GetPokerValueByNumber(x);
                int b = GetPokerValueByNumber(y);
                return b.CompareTo(a);
            });
        }

        int GetPokerValueByNumber(Poker p)
        {
            //获取扑克的牌面
            int value = (int)p.p_Number * 10;
            return value - (int)p.p_Color;
        }
    }
}